import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../components/Login'
import Home from '../components/Home'
import Welcome from '../components/Welcome'
import Employee from '../components/user/employee'
import Adminlist from '../components/user/adminlist'
import Words from '../components/words/words'
import RoleList from '../components/role/rolelist'
import Main from '../pages/main'
//导入进度条插件
import NProgress from 'nprogress'
//导入进度条样式
import 'nprogress/nprogress.css'
import nProgress from 'nprogress'
Vue.use(VueRouter)

const routes = [
  { path: '/', redirect: '/login' },
  { path: '/login', component: Login, meta: { title: 'Vue Demo Login' } },
  { path: '/main', component: Main },
  {
    path: '/home',
    component: Home,
    meta: {
      title: 'Vue Demo Home'
    },
    redirect: '/welcome',
    children: [
      {
        path: '/welcome',
        component: Welcome,
        meta: {
          title: 'Vue Demo Home'
        },
      },
      {
        path: '/employee',
        component: Employee,
        meta: {
          title: 'Vue Demo Employee List'
        },
      },
      {
        path: '/adminlist',
        component: Adminlist,
        meta: {
          title: 'Vue Demo Admim List'
        },
      },
      {
        path: '/rolelist',
        component: RoleList,
        meta: {
          title: 'Vue Demo Role List'
        }
      },
      {
        path: '/words',
        component: Words,
        meta: {
          title: 'Vue Demo Words'
        },
      }
    ]
  }
]

const router = new VueRouter({
  routes
})

router.beforeEach((to, from, next) => {
  NProgress.start();
  if (to.path === '/login')
    return next();

  if (to.path === '/main')
    return next();
  //获取token
  const tokenStr = window.sessionStorage.getItem('token');
  if (!tokenStr) {
    nProgress.done();
    return next('/login');
  }


  next();

})

router.afterEach(() => {
  NProgress.done();
  // 在即将进入新的页面组件前，关闭掉进度条
})

const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}

export default router
